var api = {
  cdn: 'https://keju123.oss-cn-shanghai.aliyuncs.com/',
  lazyload: '/static/img/default-lazyload.png',
  avatar: '/static/img/avatar-default.png',
  pathLogin: '/pages/login/login',
}


var links = function (str) {
  var REGEXP = new RegExp('\s', 'g');
  //转为小写并去除左右空格
  str = str.toLowerCase().replace(REGEXP, "");

  if (str.indexOf('https') === 0 || str.indexOf('http') === 0) {
    return "/pages/web/web?id=" + str
  } else {
    return "/pages/goods/goods?id=" + str
  }

  //  else if (str.indexOf('za://') == 0) {
  //   console.log(str);
  //   var array = str.split('_');
  //   console.log(array);
  //   if (array[0] == 'column') {
  //     gocolumn1(array[1]);
  //   }
  // }

}

//空数组
var emptyArray = function (str) {
  if (typeof (str) !== 'undefined' && str > 0) {
    var arr = [];
    for (var i = 0; i < str; i++) {
      arr.push(i);
    }
    return arr;
  }
}

//图片大小
var thumbSize = function (str) {
  if (typeof (str) !== 'undefined') {
    return '?x-oss-process=image/resize,m_fill,h_' + str + ',w_' + str
  }
}

var imageOSS = function (str) {
  if (str && typeof (str) !== 'undefined' && typeof (str) !== null) {
    if (str.indexOf('http') === 0 || str.indexOf('https') === 0) {
      return str
    } else {
      return api.cdn + str
    }
  } else {
    return api.lazyload
  }
}

// 判断后缀
var getUrlSuffix = function(url) {
  var parts = url.split('.');
  return parts.pop();
}

//保留小数
var toFix = function (value, num) {
  if (typeof (value) !== 'undefined') {
    return value.toFixed(num);
  }
}

//保留小数
var toFixKM = function (value) {
  if (typeof (value) !== 'undefined') {
    if (value < 1) {
      value = value * 100;
      return value.toFixed(2) + " 米";
    } else
      return value.toFixed(2) + " 公里";
  }
}

//保留整数
var toParseInt = function (value) {
  if (typeof (value) !== 'undefined') {
    return parseInt(value);
  }
}

//数量进两位
var rideTwo = function (value) {
  if (typeof (value) !== 'undefined') {
    return Number(value) * 100
  }
}

//10进制数
var decimal = function (value) {
  if (typeof (value) !== 'undefined') {
    return value < 10 ? '0' + value : value;
  }
}

//过滤标签
var htmlFilter = function (str) {
  var str = str.replace(new RegExp('<\/?[^>]*>', 'g'), ''); //去除HTML Tag
  str = str.replace(new RegExp('[|]*\n', ''), ''); //去除行尾空格
  str = str.replace(new RegExp('&npsp;', 'ig'), ''); //去掉npsp
  return str;
}

//处理昵称
var nickName = function (str) {
  if (typeof (str) !== 'undefined') {
    return str.replace(new RegExp('^(.).+(.)$', 'g'), '$1***$2');
  }
}

//电话号码处理
var phoneNumber = function (str) {
  if (typeof (str) !== 'undefined') {
    return str.replace(new RegExp('(\d{3})\d{4}(\d{4})', 'g'), '$1****$2');
  }
}

//头像处理 
var imageAvatar = function (str) {
  if (typeof (str) !== 'undefined' && str != "") {
    if (str.indexOf('http') === 0 || str.indexOf('https') === 0) {
      return str
    } else if (str.toLowerCase().indexOf('users') === 0) {
      return api.cdn + str + thumbSize(100);
    } else {
      return str;
    }
  } else {
    return api.avatar
  }
}

//店铺头像
var shopLogo = function (str) {
  if (str !== 'undefined' && str != null && str != '' && str != 'default_user_0.png') {
    if (str.indexOf('http') === 0 || str.indexOf('https') === 0) {
      return str
    } else if (str.toLowerCase().indexOf('shop') === 0 || str.toLowerCase().indexOf('upload') === 0) {
      return api.cdn + str + thumbSize(100);
    } else {
      return str;
    }
  } else {
    return api.lazyload
  }
}

//评分
var shopStar = function (str) {
  return str < 3 ? '低' : str >= 4 ? '高' : '中';
}

//优惠券过期
var couponOverdue = function (str) {
  var end = (getDate(str)).getTime();
  var today = getDate().getTime();
  return end - today;
}

//时间格式
var formatNumber = function (n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

var regYear = new RegExp("(y+)", "i");

var dateFormat = function (date, format) {
  if (!format) {
    format = "yyyy-MM-dd hh:mm:ss";
  }

  date = date.replace(new RegExp('-', 'g'), '/');
  // #ifdef MP-WEIXIN
  var realDate = getDate(date);
  // #endif
  
  // #ifdef MP-TOUTIAO
  let realDate = new Date(date);
  // #endif

  function timeFormat (num) {
    return num < 10 ? '0' + num : num;
  }
  var date = [
    ["M+", timeFormat(realDate.getMonth() + 1)],
    ["d+", timeFormat(realDate.getDate())],
    ["h+", timeFormat(realDate.getHours())],
    ["m+", timeFormat(realDate.getMinutes())],
    ["s+", timeFormat(realDate.getSeconds())],
    ["q+", Math.floor((realDate.getMonth() + 3) / 3)],
    ["S+", realDate.getMilliseconds()],
  ];
  var reg1 = regYear.exec(format);
  if (reg1) {

    format = format.replace(reg1[1], (realDate.getFullYear() + '').substring(4 - reg1[1].length));
  }
  for (var i = 0; i < date.length; i++) {
    var k = date[i][0];
    var v = date[i][1];

    var reg2 = new RegExp("(" + k + ")").exec(format);
    if (reg2) {
      format = format.replace(reg2[1], reg2[1].length == 1 ?
        v : ("00" + v).substring(("" + v).length));
    }
  }
  return format;
}

//默认登录
var pathLogin = function (path, logged) {
  if (logged) {
    return path
  } else {
    return api.pathLogin
  }
}

const MyPlugin = {
	install(Vue){
		Vue.prototype.$imageOSS = imageOSS;
		Vue.prototype.$links = links;
	}
};

module.exports = {
  links: links,
  thumbSize: thumbSize,
  imageOSS: imageOSS,
  emptyArray: emptyArray,
  toFix: toFix,
  toFixKM: toFixKM,
  toParseInt: toParseInt,
  rideTwo: rideTwo,
  decimal: decimal,
  htmlFilter: htmlFilter,
  nickName: nickName,
  phoneNumber: phoneNumber,
  imageAvatar: imageAvatar,
  shopLogo: shopLogo,
  shopStar: shopStar,
  couponOverdue: couponOverdue,
  dateFormat: dateFormat,
  pathLogin: pathLogin,
  MyPlugin,
  getUrlSuffix:getUrlSuffix
}